package com.we823.project.repository.service.repo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.we823.project.model.repo.MaterialStockModel;
import com.we823.project.repository.dao.repo.MaterialStockRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("materialStockService")
public class MaterialStockService extends
		AbstractBaseService<MaterialStockModel> {

	private MaterialStockRepository materialStockRepository;

	@Autowired
	public MaterialStockService(MaterialStockRepository materialStockRepository) {
		super(materialStockRepository);
		this.materialStockRepository = materialStockRepository;
	}

	public MaterialStockModel findByMaterialCodeAndYearAndMonthAndDay(
			String materialCode, int year, int month, int day) {
		return materialStockRepository.findByMaterialCodeAndYearAndMonthAndDay(
				materialCode, year, month, day);
	}

	public List<MaterialStockModel> findByMaterialCodeAndYear(
			String materialCode, int year) {
		return materialStockRepository.findByMaterialCodeAndYear(materialCode,
				year);
	}

	public List<MaterialStockModel> findByMaterialCodeAndYear(
			String materialCode, int year, Sort sort) {
		return materialStockRepository.findByMaterialCodeAndYear(materialCode,
				year, sort);
	}

	public List<MaterialStockModel> findByMaterialCode(String materialCode,
			Sort sort) {
		return materialStockRepository.findByMaterialCode(materialCode, sort);
	}

	public List<MaterialStockModel> findByYearAndMonthAndMaterialCodeIn(
			int year, int month, List<String> materialCodeList, Sort sort) {
		return materialStockRepository.findByYearAndMonthAndMaterialCodeIn(
				year, month, materialCodeList, sort);
	}

	public Page<MaterialStockModel> findByMaterialCode(String code,
			Pageable pageable) {
		return materialStockRepository.findByMaterialCode(code, pageable);
	}

	public Page<MaterialStockModel> findByMaterialCodeStartWith(String code,
			PageRequest pageRequest) {
		return materialStockRepository.findByMaterialCodeStartingWith(code,
				pageRequest);
	}

	public Page<MaterialStockModel> findByMaterialCodeIn(
			List<String> materialCodeList, PageRequest pageRequest) {
		if (pageRequest != null) {
			return materialStockRepository.findByMaterialCodeIn(
					materialCodeList, pageRequest);
		} else {
			return new PageImpl<MaterialStockModel>(
					materialStockRepository
							.findByMaterialCodeIn(materialCodeList));
		}
	}

	public List<MaterialStockModel> findByYearAndMaterialCodeIn(int year,
			List<String> materialCodeList) {
		return materialStockRepository.findByYearAndMaterialCodeIn(year,
				materialCodeList);
	}

	public List<MaterialStockModel> findByYearAndMonthAndMaterialCodeIn(
			int year, int month, List<String> materialCodeList) {
		return materialStockRepository.findByYearAndMonthAndMaterialCodeIn(
				year, month, materialCodeList);
	}

	public Page<MaterialStockModel> findByYearAndMonthAndDayMaterialCodeIn(
			int year, int month, int day, List<String> materialCodeList,
			Pageable pageable) {
		if (pageable != null) {
			return materialStockRepository
					.findByYearAndMonthAndDayAndMaterialCodeIn(year, month,
							day, materialCodeList, pageable);
		} else {
			return new PageImpl<MaterialStockModel>(
					materialStockRepository
							.findByYearAndMonthAndDayAndMaterialCodeIn(year,
									month, day, materialCodeList));
		}
	}

	public void save(List<MaterialStockModel> materialStockModels) {
		materialStockRepository.save(materialStockModels);
	}

}
